﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MINERP.GroupMember
{
    public class Common
    {
        //调用API打开PS
        [DllImport("shell32.dll")]
        public static extern int ShellExecute(int hwnd, string lpszOp, string lpszFile, string lpszParams, string lpszDir, int FsShowCmd);
        /// <summary>
        /// 打开PS
        /// </summary>
        public static void OpenPS()
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            //操作注册表
            RegistryKey userPhotoShopKey = Registry.CurrentUser.OpenSubKey("userPhotoShopKey", true);
            string psPath = "";
            if (userPhotoShopKey != null)
                psPath = userPhotoShopKey.GetValue("psPhoto", "").ToString();
            int r1 = ShellExecute(0, "open", "Photoshop.exe", null, psPath, 1);
            if (r1 == 0)
            {
                int r = ShellExecute(0, "open", "Photoshop.exe", null, "", 1);
                if (r == 0)
                {
                    MessageBox.Show("PhotoShop的路径不正确,请重新设置", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                    {
                        string newPsPath = folderBrowserDialog1.SelectedPath;
                        if (userPhotoShopKey == null)
                            userPhotoShopKey = Registry.CurrentUser.CreateSubKey("userPhotoShopKey");
                        userPhotoShopKey.SetValue("psPhoto", newPsPath);
                        int r3 = ShellExecute(0, "open", "Photoshop.exe", null, newPsPath, 1);
                        if (r3 == 0)
                        {
                            MessageBox.Show("不能打开PhotoShop,请确认是否已正确安装!", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                }
            }
            if (userPhotoShopKey != null)
                userPhotoShopKey.Close();
        }
    }
}
